import axios from 'axios'
import {TYPES_URL} from '../../components/Plug/URL'
const Types = {
	namespaced:true,
	state:{
		MsbdList:[],
		loading:false,
		pageObj:{
			currentPage:1,
			total:null
		},
		isupdata:false,
		idarr:[],
		zeroarr:[],
		// pidarr:[],
		// pidarr2:[]
	},
	getters:{},
	mutations:{
		setotal(state,payload){
			state.pageObj.total=payload
		},
		setcurrent(state,payload){
			state.pageObj.currentPage=payload
		},
		setisupdata(state,payload){
			state.isupdata=payload
		},
		setidarr(state,payload){
			let arr=[]
			for(var i=0;i<payload.length;i++){
				arr.push(payload[i].id)
			}
			state.idarr=arr
		}
    },
    actions:{
		getMsbdList({state,commit}){
			state.loading=true
			axios({
				method: 'get', 
				url: TYPES_URL,	
				// url:'http://localhost/zx1812/classify.php/rywtypes',
				params: { transform: 1, order: 'id,desc', page: `${state.pageObj.currentPage},10`},
			}).then(data => {
				state.loading=false
				state.MsbdList = data.data.records
				commit('setotal',data.data.results)
			})
		},
		getZero:{
			root:true,
			handler({rootState,state,commit}){
				axios({
					method: 'get',
					url: TYPES_URL,
					params: { transform: 1, order: 'id,desc', page: `${state.pageObj.currentPage},10`,filter:'pid,eq,0'},
				}).then(data => {
					rootState.zeroarr = data.data.records
				})
			}
		},
		getpid:{
			root:true,
			handler({rootState,state,commit},pid){
				axios({
					method: 'get',
					url: TYPES_URL,
					params: { transform: 1, order: 'id,desc', page: `${state.pageObj.currentPage},10`,filter:`pid,eq,${pid}`},
				}).then(data => {
					rootState.zeroarr2 = data.data.records
				})
			}
		},
		getpid2({state,commit},pid){
			axios({
				method: 'get',
				url: TYPES_URL,
				params: { transform: 1, order: 'id,desc', page: `${state.pageObj.currentPage},10`,filter:`pid,eq,${pid}`},
			}).then(data => {
				state.pidarr2 = data.data.records
			})
		},
		jumpPage({state,commit,dispatch},page){
			commit('setcurrent',page)
			dispatch('getMsbdList')
		},
		save({state,commit,dispatch},form){
			return new Promise((proslve,project)=>{
				let data={title:form.title}
				if(form.select2){
					data.pid=form.select2
				}else if(form.select){
					data.pid=form.select
				}else{
					data.pid=0
				}

				let method='post'
				let url=TYPES_URL
				if(state.isupdata){
					url=TYPES_URL+'/'+form.id
					method='put'
				}
				axios({
					method,
					url,
					data,
				}).then(data => {
					dispatch('getMsbdList')
					proslve()
				})
			})
		},
		delete({state,commit,dispatch},form){
			return new  Promise((proslve,project)=>{
				commit('setidarr',form)
				axios({
					method:'delete',
					url:TYPES_URL+'/'+state.idarr.join(','),
				}).then(data => {
					dispatch('getMsbdList')
					proslve()
				})
			})
		},
		fiflter({state,commit,dispatch},form){
			state.pageObj.currentPage=1
			let fifterval=''
			if(form.select=='all'){
				fifterval='cs,'
			}else if(form.select2){
				fifterval=`eq,${form.select2}` 
			}else{
				fifterval=`eq,${form.select}`
			}
			state.loading=true
			axios({
				method: 'get',
				url: TYPES_URL,
				params: { transform: 1, order: 'id,desc', page: `${state.pageObj.currentPage},10`,filter:`pid,${fifterval}`},
			}).then(data => {
				state.loading=false
				state.MsbdList = data.data.records
				commit('setotal',data.data.results)
			}).then(()=>{
				if(form.select&&form.select!='all'&&!form.select2){
					for(var i=0;i<state.MsbdList.length;i++){
						axios({
							method: 'get',
							url: TYPES_URL,
							params: { transform: 1, order: 'id,desc', page: `${state.pageObj.currentPage},10`,filter:`pid,eq,${state.MsbdList[i].id}`},
						}).then(data => {
							state.MsbdList=state.MsbdList.concat(data.data.records)
						})
					}
				}
			})
		}
	}
}
export default Types